<?php

require_once(MY_ROOT."/models/common/CFile.php");

require_once(MY_ROOT."/models/common/CString.php");

require_once(MY_ROOT."/models/common/CFilter.php");

class CUpload 
{
	public $goodtogo;
	
	public $error;
	
	public $allowedmimes=array("image/pjpeg","image/gif","image/x-png","image/bmp","image/jpg","image/png","image/jpeg");
	public $allow = array('.gif','.jpg','.jpeg','.png');
	public function __construct(){
	
	}
	public function get_filetype($path)
    {
        $pos = strrpos($path, '.');
        if ($pos !== false){
            return strtolower(substr($path, $pos));
        }
        else{
            return '';
        }
    }
	
	public function setError($error){
		
		$this->error=$error;
		
	}

	public function CUpload(){
			$this->__construct();
	}

	
	public function uploadFile($path,$cutpath=NULL,$size=NULL,$maxSize=NULL){

		if(true){
				if(!@opendir($path)){
					$isTrue=CFile::createFolder($path);
					if(!$isTrue){
						$this->goodtogo=false;
						$this->setError("新建文件夹错误!");
						return $this->error;
					}
				}
				$this->goodtogo=true;
				$i=0;
				$j=0;

				$thisFile=$_FILES;
				$fileCounts=0;
				
				foreach($thisFile as $item){
					if(!is_array($item['size'])){
						if ($item['name']) {
							if(!in_array($this->get_filetype($item['name']),$this->allow)){
								$j++;
							}
						}
					} else {
						foreach($item as $key=>$item2){
							if($key=="name"){
								foreach($item2 as $item3){									
									if ($item3) {										
										if(!in_array($this->get_filetype($item3),$this->allow)){
											
											$j++;
										}
									}
								}
							}
						}
					}
				}
				foreach($thisFile as $item){
						if(!is_array($item['size'])){
							if($maxSize!=NULL){
								if($item['size']>$maxSize){
									$this->setError("您上傳的文件大于指定的大小,請返回修改!");
									return $this->error;
								}
							}
							if($item['size']==0){
									$i++;
							}
							else if(!in_array($item['type'],$this->allowedmimes)){
									$j++;
							}
							$fileCounts++;

						}
						else{
							foreach($item as $key=>$item2){
								if($maxSize!=NULL){
									if($key=="size"){
										foreach($item2 as $item3){
											if($item3>$maxSize){
												$this->setError("您上傳的文件大于指定的大小,請返回修改!");
												return $this->error;
											}
										}
									}
								}
								if($key=="type"){
									foreach($item2 as $item3){
										if($item3==""){
											$i++;
										}
										else if(!in_array($item3,$this->allowedmimes)){
											$j++;
										}
										$fileCounts++;
									}
								}						
							}

						}
				}
				if($i==$fileCounts){
						$this->goodtogo=false;
						$this->setError("您没有选择要上传的文件,請返回修改!");
						return $this->error;
				}
				$valiFileCounts=$fileCounts-$i;
				if($j>0){
						$this->goodtogo=false;
						$this->setError("您上傳的文件格式有誤! 請重新檢查文件的格式!");
						return $this->error;
				}

				if($this->goodtogo){

						$k=0;
						$thisImgArray=array();
						foreach($thisFile as $value){
								if(!is_array($value['size'])){
									if($value['size']!=0){
											$thisTime=time();
											$thisImageName=CFilter::filterFileName($k.$thisTime.CString::randstr(8).$value['name'],"");
											if(!move_uploaded_file($value['tmp_name'],$path.$thisImageName)){
													$this->goodtogo=false;
													$this->setError("上傳文件目錄有錯誤! 請檢查屬性!");
													return $this->error;
											}
											if($this->goodtogo){
													if($cutpath!=NULL){
															$thisTypeArray=explode(".",$thisImageName);
															$thisIamge=new MImage();
															$thisIamge->saveImage($cutpath.$thisImageName,$size,$thisTypeArray[count($thisTypeArray)-1],$cutpath.$thisImageName);
													}
													$thisImgArray[]=$thisImageName;
											}
											$k++;	
									}
								}
								else{
									for($i=0;$i<$fileCounts;$i++){
										if($value['name'][$i]!=""){
											$thisTime=time();
											$thisImageName=CFilter::filterFileName($i.$thisTime.CString::randstr(8).$value['name'][$i],"");
											if(!move_uploaded_file($value['tmp_name'][$i],$path.$thisImageName)){
													$this->goodtogo=false;
													$this->setError("上傳文件目錄有錯誤! 請檢查屬性!");
													return $this->error;
											}
											if($this->goodtogo){
													if($cutpath!=NULL){
															$thisTypeArray=explode(".",$thisImageName);
															$thisIamge=new MImage();
															$thisIamge->saveImage($cutpath.$thisImageName,$size,$thisTypeArray[count($thisTypeArray)-1],$cutpath.$thisImageName);
													}
													$thisImgArray[]=$thisImageName;
											}
										}
									}


								}

						}

				}
				return $thisImgArray;
		}

	}

}